<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
    <script>
      function countingSort(arr) {
        let max = -1;
        for(let v of arr) {
          max = Math.max(max,v);
        }

        let counts = [];
        for(let i = 0;i < arr.length;i++) {
          let index = arr[i];
          if(!counts[index]) {
            counts[index] = 0;
          }
          counts[index]++;
        }

        let sortedIndex = 0;
        for(let i = 0;i<counts.length;i++) {
          while(counts[i] > 0) {
            arr[sortedIndex++] = i;
            counts[i]--;
          }
        }
        return arr;
      }
      console.log(countingSort([1,1,2,3,4,5,6,7,7,8,8,9]));
    </script>
</body>
</html>